layui.use('table', function () {

    var table = layui.table;
    //方法渲染
    table.render({
        elem: '#shopOrders',  //绑定table表格
        height: 500,
        url: '/ordersCount' //数据接口
        , page: true //开启分页
        , cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: '订单ID', width: 100, sort: true, fixed: 'left'}
                , {field: 'receiverName', title: '收货人', width: 100}
                , {field: 'receiverMobile', title: '收货人手机号', width: 130}
                , {field: 'receiverAddress', title: '收货地址', width: 130}
                , {field: 'totalAmount', title: '订单总价格', width: 100}
                , {field: 'postAmount', title: '配送费', width: 100}
                , {field: 'realPayAmount', title: '实际支付总价格', width: 130}
                , {
                field: 'payMethod',
                title: '支付方式',
                width: 130,
                templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.payMethod == '1') {
                        return '<span >微信支付</span>'
                    } else if (res.payMethod == '2') {
                        return '<span >支付宝支付</span>'
                    }
                }}

                , {field: 'isComment', title: '订单评价', width: 130,
                templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.isComment == '1') {
                        return '<span >已评价</span>'
                    } else if (res.isComment == '0') {
                        return '<span >未评价</span>'
                    }}}
                , {
                field: 'createdTime',
                title: '创建时间',
                width: 150,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
                , {
                field: 'orderStatus',
                title: '订单状态',
                width: 110,
                templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.orderStatus == '10') {
                        return '<span >待付款</span>'
                    } else if (res.orderStatus == '20') {
                        return '<span >已付款</span>'
                    }else if (res.orderStatus == '30') {
                        return '<span >已发货</span>'
                    }else if (res.orderStatus == '40') {
                        return '<span >交易成功</span>'
                    }else if (res.orderStatus == '50') {
                        return '<span >交易关闭</span>'
                    }
                }}

                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 265}
            ]
        ]
    });


    table.on('tool(AOrders)', function (obj) {  //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data;   //获得当前行数据
        // console.log("del:::" + obj)
        if (obj.event === 'del') {
            //删除数据

            //执行ajax请求
            $.ajax({
                url: '/userDelete?id=' + data.id,
                method: 'GET',
                dataType: 'text',
                success: function (data) {
                    layer.msg(data);
                    obj.del();   //删除对应行（tr）的DOM结构，并更新缓存
                }
            });
        } else if (obj.event === 'modify') {   //修改数据

            layer.open({
                type: 2,
                title: '查看订单信息'   //标题
                , area: ['500px', '300px']    //宽高
                , content: ['/lookOrders?id=' + data.id] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });

        }


    });

    $('#orderBtnSearch').click(function () {
        var key = $('#orderSltKey').val();
        var value = $('#orderEdtSearch').val();
        table.reload('shopOrders', {where: {type: key, keywords: value}});
    });
});


/**
 * 查看订单项信息
 * @param orderId
 */
function openOrderItems(orderId) {

    $.ajax({
        type: 'GET',//方法类型
        url: '/admin/order-items/' + orderId,
        contentType: 'application/json',
        success: function (result) {
            if (result.resultCode == 200) {
                $('#orderItemModal').modal('show');
                var itemString = '';
                for (i = 0; i < result.data.length; i++) {
                    itemString += result.data[i].goodsName + ' x ' + result.data[i].goodsCount + ' 商品编号 ' + result.data[i].goodsId + ";<br>";
                }

                $("#orderItemString").html(itemString);
            } else {
                swal(result.message, {
                    icon: "error",
                });
            }
            ;
        },
        error: function () {
            swal("操作失败", {
                icon: "error",
            });
        }
    });
}